<template>
  <div class="container">
    <!-- 左边:输入框 -->
    <div class="left">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 账号的 -->
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 右边:封面 -->
    <div class="right"></div>
  </div>
</template>
<script>
// @ 就是 src的意思
import Api from "@/api";
export default {
  data() {
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        username: "",
      },
      rules: {
        username: [{ validator: checkUsername, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      console.log("submitForm");
      try {
        console.log(this.$refs[formName].validate);
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 校验成功之后，我们要先拿到数据
            console.log({
              username: this.ruleForm.username,
              password: this.ruleForm.pass,
            });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } catch (error) {
        console.log(error);
      }
    },
    async submit() {
      // 校验成功之后,await调用，我们要先拿到数据
      const res = await Api.apiLogin({
        name: this.ruleForm.username,
        password: this.ruleForm.pass,
      });
      if (res.code === 1) {
        // 成功 code 1
        // 这个就是后端给你的数据
        console.log(res);
        // 先存起来
        localStorage.setItem("token", res.data.token);
        // 头像也存起来
        localStorage.setItem("avatar", res.data.user_info.head);
        // 通知用户登录成功
        this.$notify({
          title: "登录成功",
          message: "您可以开始使用拉",
          type: "success",
        });
        // 跳转首页
        this.$router.push("/home");
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
.container {
  display: flex;
  height: 100vh;
  div {
    flex: 1;
  }
  .left {
    margin: 150px 50px 0 0;
  }
  .right {
    background: url("https://img.iplaysoft.com/wp-content/uploads/2019/free-images/free_stock_photo.jpg")
      no-repeat center;
    background-size: cover;
  }
}
</style>
